<template>
  <div class="rateQuery">
    <!-- <div style="margin-left:5vw">
      <span style="font-weight: bold;font-size: 12px;">
        <p class="p_sty">
          对以下业务申请变更，请上传《商户业务变更申请表》加盖公章的照片，我们将在1-3个工作日内完成审核，审核通过后变更生效。
          <a
            href="static/商户业务变更申请表.docx"
            download="商户业务变更申请表.docx"
            target="_blank"
          >下载模板</a>
        </p>
      </span> 
    </div>-->
    <a-form @submit="handleSubmit" :form="form" style="margin:0 auto;">
      <div style="width: 100%;margin:20px auto;">
        <a-form-item v-bind="formItemLayout" label="模板名称">
          <a-input
            style="width:60%"
            v-decorator="[
                        'feename',
                        {rules: [{ required: true, message: '请输入模板名称' }]}
                      ]"
            placeholder="请输入模板名称"
          />
        </a-form-item>
        <a-table
          :columns="columns"
          :dataSource="ratelist"
          :rowClassName="rowClassName"
          :pagination="false"
          :rowKey="record => record.id"
        >
          <div slot="feeTemplate" slot-scope="record,text,index">
            <div
              v-if="record.templateBankDTOList != null && record.templateBankDTOList.length > 0"
            >按类分布</div>
            <div
              v-if="record.templateBankDTOList == null || record.templateBankDTOList.length == 0"
            >
              <!-- {{record.feeTemplate | feeTemplate}} -->
              <div v-if="record.feeTemplate.rate !=null">
                <span v-if="record.name=='D0'">D0垫资</span>
                <span v-if="record.name!='D0'">&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;&nbsp;</span>
                <a-input-number
                  :step="0.01"
                  v-model="record.feeTemplate.rate"
                  style="width:50%"
                  :min="record.minFee.rate"
                  :max="record.maxFee.rate"
                  @change="inputValue(index,record.feeTemplate.rate,record.minFee.rate)"
                />
                <span v-if="record.feeTemplate.rate !=null">%</span>

                <div v-if="record.name=='pos借记'">
                  <br />
                  <span>&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;&nbsp;</span>
                  <a-input-number
                    :min="record.minFee.top"
                    :max="record.maxFee.top"
                    :step="1"
                    v-model="record.feeTemplate.top"
                    style="width:50%"
                    @change="inputValue2(index,record.feeTemplate.top,record.minFee.top)"
                  />
                  <span>元封顶</span>
                </div>
              </div>
              <br v-if="record.name=='D0'" />
              <div v-if="record.feeTemplate.amount !=null">
                <span v-if="record.name=='D0'">D0单笔</span>
                <span v-if="record.name!='D0'">&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;&nbsp;</span>
                <a-input-number
                  :min="record.minFee.amount"
                  :max="record.maxFee.amount == 0 ? 10000000 : record.maxFee.amount"
                  :step="1"
                  v-model="record.feeTemplate.amount"
                  style="width:50%"
                  @change="inputValue3(index,record.feeTemplate.amount,record.minFee.amount)"
                />
                <span v-if="record.feeTemplate.amount != null">元/笔</span>
              </div>
            </div>
          </div>
          <!-- <div slot="status" slot-scope="text,record,index">
            <a-form-item v-bind="formItemLayout">
              <a-radio-group :value="record.status+''" @change="onChange2" :name="index +''">
                <a-radio value="true">开启</a-radio>
                <a-radio value="false">关闭</a-radio>
              </a-radio-group>
            </a-form-item>
          </div>-->
          <div
            v-if="record.templateBankDTOList.length > 0"
            slot="expandedRowRender"
            slot-scope="record"
            style="margin: 0"
          >
            <a-table
              :columns="columns2"
              :dataSource="record.templateBankDTOList"
              :rowKey="record => record.id"
              :pagination="false"
              size="small"
            >
              <div slot="cardType" slot-scope="record">
                <span v-if="record.cardType=='D'">借记卡</span>
                <span v-if="record.cardType=='C'">贷记卡</span>
              </div>
              <div slot="feeTemplate" slot-scope="record">
                <!-- {{record.feeTemplate | feeTemplate}} -->
                <div>
                  <div v-if="record.feeTemplate.rate !=null">
                    <a-input-number
                      :min="record.minFee.rate"
                      :max="record.maxFee.rate"
                      :step="0.01"
                      v-model="record.feeTemplate.rate"
                      style="width:60%"
                    />
                    <span v-if="record.feeTemplate.amount == null">%</span>
                  </div>

                  <div v-if="record.feeTemplate.amount !=null">
                    <a-input-number
                      :min="record.minFee.amount"
                      :max="record.maxFee.amount"
                      :step="1"
                      v-model="record.feeTemplate.amount"
                      style="width:60%"
                    />
                    <span v-if="record.feeTemplate.amount != null">元/笔</span>
                  </div>
                </div>
              </div>
              <div slot="transAmountTop" slot-scope="record">
                <!-- <div v-if="record.transAmountTop == 0">\</div>
                <div v-if="record.transAmountTop != 0">{{record.transAmountTop | cashfilter}}元封顶</div>-->
                <div v-if="record.transAmountTop !=null">
                  <a-input v-model="record.transAmountTop" style="width:60%" />
                  <span>元/封顶</span>
                </div>
              </div>
            </a-table>
          </div>
          <!-- <template slot='expandIcon' slot-scope="props">
      <a-icon slot='expandIcon' slot-scope="props" type="down-circle" />
          </template>-->
        </a-table>
        <a-form-item v-bind="formItemLayout" style="margin-top:20px;" label="设为默认">
          <a-select style="width:60%" v-model="defaultValue" placeholder="设为默认">
            <a-select-option value="true">是</a-select-option>
            <a-select-option value="false">否</a-select-option>
          </a-select>
        </a-form-item>
      </div>

      <div class="ratebutton">
        <a-button
          style="width:90px;margin-top:1vh;"
          html-type="submit"
          type="primary"
          :loading="loading"
        >保存</a-button>
        <!-- <a-button
          style="width:90px;margin-top:1vh;margin-left:20px;"
          @click="goback"
          type="primary"
        >取消</a-button>-->
      </div>
    </a-form>
  </div>
</template>
<script>
import AHideData from "../../../common/HideData";
import api from "@/common/api";
const columns = [
  { title: "功能名称", dataIndex: "name" },
  { title: "费率", scopedSlots: { customRender: "feeTemplate" } }
  // { title: "当前状态", scopedSlots: { customRender: "status" } }
];
const columns2 = [
  { title: "发卡行", dataIndex: "bankName" },
  { title: "卡类型", scopedSlots: { customRender: "cardType" } },
  { title: "费率", scopedSlots: { customRender: "feeTemplate" } },
  { title: "封顶金额", scopedSlots: { customRender: "transAmountTop" } }
];

export default {
  components: {
    AHideData
  },
  data() {
    return {
      ratelist: [],
      templateBankDTOList: [],
      fileList: [],
      transStatus: [],
      columns,
      columns2,
      query: {},
      feeType: "请选择",
      defaultValue: "true",
      feeName: "",
      visible: false,
      loading: false,
      issend: true,
      previewVisible: false,
      previewImage: "",
      form: this.$form.createForm(this),
      formItemLayout: {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 20 },
          md: { span: 16 }
        }
      }
    };
  },
  mounted() {
    //   this.spinning = true;
    this.getratelist();
  },
  filters: {
    cashfilter(value) {
      return parseFloat(value / 100).toFixed(2);
      // return Number(value).toFixed(2)
    },
    feeTemplate(value) {
      if (value.amount == null) {
        return (Math.round(value.rate * 1000000) / 10000).toFixed(4) + "%";
      } else {
        return value.amount / 100 + "元/笔";
      }
    }
  },
  methods: {
    inputValue(index,value,min){
      if(value == "" || value == undefined){
        this.ratelist[index].feeTemplate.rate = min
      }
    },
    inputValue2(index,value,min){
      if(value == "" || value == undefined){
        this.ratelist[index].feeTemplate.top = min
      }
    },
    inputValue3(index,value,min){
      if(value == "" || value == undefined){
        this.ratelist[index].feeTemplate.amount = min
      }
    },
    rowClassName(record, index) {
      if (record.templateBankDTOList == null || record.templateBankDTOList.length == 0) {
        return "noExpand";
      }
    },
    handlePreview(file) {
      this.previewImage = file.url || file.thumbUrl;
      this.previewVisible = true;
    },
    handleCancel() {
      this.previewVisible = false;
    },
    handleChange({ file, fileList }) {
      let pos = file.name.lastIndexOf(".");
      let lastName = file.name.substring(pos, file.name.length);
      let supportTypes = [".jpeg", ".png", ".jpg"]; // jpeg、 png
      if (supportTypes.indexOf(lastName.toLowerCase()) <= -1) {
        this.$message.error("上传图片仅支持 jpeg、 png 2种格式！");
        return;
      }
      if (file.size / (1024 * 1024) > this.imageSize) {
        this.$message.error(`上传图片大小应小于${this.imageSize}Mb`);
        return;
      }
      this.fileList = fileList;
      let obj =
        fileList.length > 0
          ? fileList[0]
          : { response: { data: null, code: null } };
      if (obj) {
        if (obj.response) {
          if (obj.response.code == "OK") {
            console.log(obj.response.data);
          }
        }
      }
    },
    goback() {
      this.$router.push("/org/transmanage/rateTemplate");
    },
    getratelist() {
      this.$axios.get("/agent/fee/group/getDefaultAgentFee").then(res => {
        console.log("rate", res);
        if (res.data.code == "OK") {
          this.ratelist = res.data.list;
          for (let idx in this.ratelist) {
            //默认费率
            this.ratelist[idx].feeTemplate = JSON.parse(
              this.ratelist[idx].feeTemplate
            );
            if (this.ratelist[idx].feeTemplate.rate != null) {
              this.ratelist[idx].feeTemplate.rate =
                Math.round(this.ratelist[idx].feeTemplate.rate * 1000000) /
                10000;
              if (this.ratelist[idx].name == "D0") {
                this.ratelist[idx].feeTemplate.amount =
                  this.ratelist[idx].feeTemplate.amount / 100;
              }
              if (this.ratelist[idx].name == "pos借记") {
                this.ratelist[idx].feeTemplate.top =
                  this.ratelist[idx].feeTemplate.top / 100;
              }
            } else {
              this.ratelist[idx].feeTemplate.amount =
                this.ratelist[idx].feeTemplate.amount / 100;
            }
            //最大值费率
            this.ratelist[idx].maxFee = JSON.parse(this.ratelist[idx].maxFee);
            if (this.ratelist[idx].maxFee.rate != null) {
              this.ratelist[idx].maxFee.rate =
                Math.round(this.ratelist[idx].maxFee.rate * 1000000) / 10000;
              if (this.ratelist[idx].name == "D0") {
                this.ratelist[idx].maxFee.amount =
                  this.ratelist[idx].maxFee.amount / 100;
              }
              if (this.ratelist[idx].name == "pos借记") {
                this.ratelist[idx].maxFee.top =
                  this.ratelist[idx].maxFee.top / 100;
              }
            } else {
              this.ratelist[idx].maxFee.amount =
                this.ratelist[idx].maxFee.amount / 100;
            }
            //最小值费率
            this.ratelist[idx].minFee = JSON.parse(this.ratelist[idx].minFee);
            if (this.ratelist[idx].minFee.rate != null) {
              this.ratelist[idx].minFee.rate =
                Math.round(this.ratelist[idx].minFee.rate * 1000000) / 10000;
              if (this.ratelist[idx].name == "D0") {
                this.ratelist[idx].minFee.amount =
                  this.ratelist[idx].minFee.amount / 100;
              }
              if (this.ratelist[idx].name == "pos借记") {
                this.ratelist[idx].minFee.top =
                  this.ratelist[idx].minFee.top / 100;
              }
            } else {
              this.ratelist[idx].minFee.amount =
                this.ratelist[idx].minFee.amount / 100;
            }
            if (this.ratelist[idx].templateBankDTOList != null) {
              for (let idx2 in this.ratelist[idx].templateBankDTOList) {
                //拓展默认费率
                this.ratelist[idx].templateBankDTOList[
                  idx2
                ].feeTemplate = JSON.parse(
                  this.ratelist[idx].templateBankDTOList[idx2].feeTemplate
                );
                this.ratelist[idx].templateBankDTOList[idx2].transAmountTop =
                  this.ratelist[idx].templateBankDTOList[idx2].transAmountTop /
                  100;
                if (
                  this.ratelist[idx].templateBankDTOList[idx2].feeTemplate
                    .rate != null
                ) {
                  this.ratelist[idx].templateBankDTOList[
                    idx2
                  ].feeTemplate.rate =
                    this.ratelist[idx].templateBankDTOList[idx2].feeTemplate
                      .rate * 100;
                } else {
                  this.ratelist[idx].templateBankDTOList[
                    idx2
                  ].feeTemplate.amount =
                    this.ratelist[idx].templateBankDTOList[idx2].feeTemplate
                      .amount / 100;
                }

                //拓展最大费率
                this.ratelist[idx].templateBankDTOList[
                  idx2
                ].maxFee = JSON.parse(
                  this.ratelist[idx].templateBankDTOList[idx2].maxFee
                );
                if (
                  this.ratelist[idx].templateBankDTOList[idx2].maxFee.rate !=
                  null
                ) {
                  this.ratelist[idx].templateBankDTOList[idx2].maxFee.rate =
                    this.ratelist[idx].templateBankDTOList[idx2].maxFee.rate *
                    100;
                } else {
                  this.ratelist[idx].templateBankDTOList[idx2].maxFee.amount =
                    this.ratelist[idx].templateBankDTOList[idx2].maxFee.amount /
                    100;
                }

                //拓展最小费率
                this.ratelist[idx].templateBankDTOList[
                  idx2
                ].minFee = JSON.parse(
                  this.ratelist[idx].templateBankDTOList[idx2].minFee
                );
                if (
                  this.ratelist[idx].templateBankDTOList[idx2].minFee.rate !=
                  null
                ) {
                  this.ratelist[idx].templateBankDTOList[idx2].minFee.rate =
                    this.ratelist[idx].templateBankDTOList[idx2].minFee.rate *
                    100;
                } else {
                  this.ratelist[idx].templateBankDTOList[idx2].minFee.amount =
                    this.ratelist[idx].templateBankDTOList[idx2].minFee.amount /
                    100;
                }
              }
            }
          }

          console.log("this.ratelist", this.ratelist);
          // this.spinning = false;
        } else {
          // this.spinning=false;
        }
      });
    },
    onChange2(e) {
      console.log("radio checked", e.target.value);
      console.log("radio checked", e.target.name);
      let index = Number(e.target.name);
      if (e.target.value == "true") {
        this.ratelist[index].status = true;
      } else {
        this.ratelist[index].status = false;
      }
    },
    handleSubmit(e) {
      console.log(e);
      e.preventDefault();
      this.form.validateFields((err, values) => {
        if (!err) {
          for (let i = 0; i < this.ratelist.length; i++) {
            if (this.ratelist[i].name != "D0") {
              if (this.ratelist[i].feeTemplate.rate != null) {
                if (
                  this.ratelist[i].feeTemplate.rate >
                    this.ratelist[i].maxFee.rate ||
                  this.ratelist[i].feeTemplate.rate <
                    this.ratelist[i].minFee.rate
                ) {
                  this.$message.info(this.ratelist[i].name + "费率超过限制");
                  return false;
                }
                if (this.ratelist[i].name == "pos借记") {
                  if (
                    this.ratelist[i].feeTemplate.rate >
                      this.ratelist[i].maxFee.rate ||
                    this.ratelist[i].feeTemplate.rate <
                      this.ratelist[i].minFee.rate
                  ) {
                    this.$message.info(this.ratelist[i].name + "费率超过限制");
                    return false;
                  }
                  if (
                    this.ratelist[i].feeTemplate.top >
                      this.ratelist[i].maxFee.top ||
                    this.ratelist[i].feeTemplate.top <
                      this.ratelist[i].minFee.top
                  ) {
                    this.$message.info(this.ratelist[i].name + "费率超过限制");
                    return false;
                  }
                }
              } else {
                if (
                  this.ratelist[i].feeTemplate.amount >
                    this.ratelist[i].maxFee.amount ||
                  this.ratelist[i].feeTemplate.amount <
                    this.ratelist[i].minFee.amount
                ) {
                  this.$message.info(this.ratelist[i].name + "费率超过限制");
                  return false;
                }
              }
            } else {
              if (
                this.ratelist[i].feeTemplate.rate >
                  this.ratelist[i].maxFee.rate ||
                this.ratelist[i].feeTemplate.rate < this.ratelist[i].minFee.rate
              ) {
                this.$message.info("D0垫资超过限制");
                return false;
              }
              if(this.ratelist[i].maxFee.amount != 0){
                if (
                  this.ratelist[i].feeTemplate.amount >
                    this.ratelist[i].maxFee.amount ||
                  this.ratelist[i].feeTemplate.amount <
                    this.ratelist[i].minFee.amount
                ) {
                  this.$message.info("D0单笔超过限制");
                  return false;
                }
              }
            }
          }
          let feeTemplateAddRequestList = [];
          this.ratelist.forEach((item, itemIndex) => {
            let rate1 = 0;
            let strfeeTemplate = "";
            if (item.name != "D0") {
              if (item.feeTemplate.rate != null) {
                rate1 = Math.round(item.feeTemplate.rate * 10000) / 1000000;
                if (item.name == "pos借记") {
                  let top = Math.round(item.feeTemplate.top * 100);
                  strfeeTemplate = '{"rate":' + rate1 + ',"top":' + top + "}";
                } else {
                  strfeeTemplate = '{"rate":' + rate1 + "}";
                }
              } else {
                rate1 = Math.round(item.feeTemplate.amount * 100);
                strfeeTemplate = '{"amount":' + rate1 + "}";
              }
            } else {
              let D0amount = 0;
              rate1 = Math.round(item.feeTemplate.rate * 10000) / 1000000;
              D0amount = Math.round(item.feeTemplate.amount * 100);
              strfeeTemplate =
                '{"rate":' +
                rate1 +
                ',"amount":' +
                D0amount +
                ',"settle_percent":' +
                item.feeTemplate.settle_percent +
                "}";
            }

            let data = {
              id: item.id,
              feeTemplate: strfeeTemplate,
              feeTemplateBankAddRequestList: null
            };
            let feeTemplateBankAddRequestList = [];
            if (
              item.templateBankDTOList != null &&
              item.templateBankDTOList.length > 0
            ) {
              item.templateBankDTOList.forEach((itemBank, itemBankIndex) => {
                let rate2 = 0;
                let strfeeTemplate1 = "";
                if (itemBank.feeTemplate.rate != null) {
                  rate2 =
                    Math.round(itemBank.feeTemplate.rate * 10000) / 1000000;
                  strfeeTemplate1 = '{"rate":' + rate2 + "}";
                } else {
                  rate2 = itemBank.feeTemplate.amount * 100;
                  strfeeTemplate1 = '{"amount":' + rate2 + "}";
                }

                let dataBank = {
                  id: itemBank.id,
                  transAmountTop: itemBank.transAmountTop * 100,
                  feeTemplate: strfeeTemplate1
                };
                feeTemplateBankAddRequestList[itemBankIndex] = dataBank;
              });
            }
            if (feeTemplateBankAddRequestList.length > 0) {
              data.feeTemplateBankAddRequestList = feeTemplateBankAddRequestList;
            } else {
              data.feeTemplateBankAddRequestList = null;
            }

            feeTemplateAddRequestList[itemIndex] = data;
          });
          if (this.defaultValue == "true") {
            this.defaultValue = true;
          } else {
            this.defaultValue = false;
          }
          console.log(feeTemplateAddRequestList);
          console.log(this.defaultValue);
          this.loading = true;
          this.$axios
            .post("/agent/fee/group/addFeeTemplateGroup", {
              code: "agent",
              name: values.feename,
              defaultValue: this.defaultValue,
              feeTemplateAddRequestList: feeTemplateAddRequestList
            })
            .then(
              res => {
                this.loading = false;
                console.log(res);
                if (res.data.code == "OK") {
                  this.$router.push("/org/transmanage/rateTemplate");
                } else {
                  this.$message.warning(res.data.message);
                }
              },
              err => {
                this.loading = false;
                this.$message.error(err.message);
              }
            );
        }
      });
    }
  }
};
</script>
<style lang="less" scoped>
/deep/.noExpand {
  .ant-table-row-expand-icon {
    display: none;
  }
}
.rateQuery {
  overflow: scroll;
  height: 100%;
  // padding: 30px;
  overflow-x: hidden;
  overflow-y: auto;
}
/*滚动条样式*/
.rateQuery::-webkit-scrollbar {
  /*滚动条整体样式*/
  width: 4px;
  /*高宽分别对应横竖滚动条的尺寸*/
  height: 4px;
}

.rateQuery::-webkit-scrollbar-thumb {
  /*滚动条里面小方块*/
  border-radius: 5px;
  box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
  -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
  background: rgba(0, 0, 0, 0.2);
}

.rateQuery::-webkit-scrollbar-track {
  /*滚动条里面轨道*/
  box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
  -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
  border-radius: 0;
  background: rgba(0, 0, 0, 0.1);
}
.ratebutton {
  text-align: center;
}
</style>